package com.xk._01基础篇._01复杂度;

import com.xk.tools.Times;

/**
 * @description:
 * @author: xu
 * @date: 2022/9/14 17:59
 */
public class Main {
    public static void main(String[] args) {
        int n = 46;
        Times.test("fib1", () -> System.out.println(fib1(n)));
        System.out.println("===================================");
        Times.test("fib2", () -> System.out.println(fib2(n)));
    }

    /*
       0 1 1 2 3 5 8 13 ......
     */
    public static int fib1(int n){
        if (n <= 1) return n;
        return fib1(n-1) + fib1(n-2);
    }

    public static int fib2(int n){
        if (n <= 1) return n;
        int first = 0;
        int second = 1;
        for (int i = 0; i < n - 1; i++){
            int sum = first + second;
            first = second;
            second = sum;
        }
        return second;
    }
}
